<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Products extends CI_Controller {

        public function index()
        {
            $this->load->model('Products_model');            
            $this->load->helper(array('form', 'url'));            
            $code = null;
            $description = null;
            $category = null;

            if ($this->input->post('code')) {
                $code = $this->input->post('code');
            }
            if ($this->input->post('description')) {
                $description = $this->input->post('description');
            }
            if ($this->input->post('category')) {
                $category = $this->input->post('category');
            }
            $products = $this->Products_model->getAll();
            if ($code != null || $description != null || $category != null) {
                $products = $this->Products_model->search($code, $description, $category);
            }

            $code_all_products = $this->Products_model->getAll();
            
            $vars['code_all_products'] = $code_all_products;            
            $vars['products'] = $products;
            $vars['code'] = $code;
            $vars['description'] = $description;
            $vars['category'] = $category;
            $vars['content_view'] = 'products_list_index';
            $vars['menu'] = 'products';
             
            $this->load->view('template', $vars);
        }
        
        public function add()
        {
            $this->load->model('Product_groups_model');
            $vars['productGroups'] = $this->Product_groups_model->getAll();
            $vars['content_view'] = 'products_form';
            $vars['menu'] = 'products';
            $this->load->view('template', $vars);
        }
        
        public function edit($code)
        {
            $this->load->model('Products_model');
            $this->load->model('Product_groups_model');
            $vars['productGroups'] = $this->Product_groups_model->getAll();
            $vars['product'] = $this->Products_model->find2($code);
            $vars['content_view'] = 'products_form';
            $vars['menu'] = 'products';
            $this->load->view('template', $vars);
        }
        
        public function delete($code)
        {
            $this->load->model('Products_model');
        $this->Products_model->delete($code);
        $this->index();
        }
        
        public function evalue()
        {
            if ($this->input->post('code')) {
                $code = $this->input->post('code');
                $this->load->model('Products_model');
                $dataProduct = $this->Products_model->find($code);
                if ($dataProduct) {
                    echo "true";
                } else {
                    echo "false";
                }
            }
        }
        
        public function submit()
        {
            $id = $this->input->post('products_id');
            $estado = $this->input->post('estado');             
            if ($this->_validate_submit($id) === FALSE) {
                
                if ($estado == "Agregar") {
                    $this->add();                   
                } else {
                    $this->edit($id);                                       
                }
                return;
            } else {
                $this->load->model('Products_model');
                $data = array();
                $data['code'] = $this->input->post('code');
                $data['description'] = $this->input->post('description');
                $data['group_id'] = $this->input->post('group_id');
                $data['insert_date'] = $this->input->post('insert_date');
                $data['status'] = 1;
                $data['pack_desc'] = $this->input->post('pack_desc');
                $data['pack_units'] = $this->input->post('pack_units');
                $data['box_packs'] = $this->input->post('box_packs');
                $data['cost'] = $this->input->post('cost');
                $data['price'] = $this->input->post('price');
                
                if($_FILES['path_image']['name']){
                    $prefix = substr(md5(uniqid(rand())),0,6);
                    $dir = "images/products/".$prefix.$_FILES['path_image']['name'];
                    if (move_uploaded_file($_FILES['path_image']['tmp_name'], $dir)){
                        $success_message = "Upload file! ";
                    }else{
                        $message_error = "Error to upload file";
                    }
                    $data['picture'] = str_replace("images/products/", "", $dir);
                }
            }
            if ($estado == "Agregar") {
                $this->Products_model->add($data);   
                
            } else {                
                $this->Products_model->update($id, $data);                
            }
            redirect('/products/', 'refresh');
        }
        
        private function _validate_submit($id = null)
        {
           
            if (!isset($id)) {
                $this->form_validation->set_rules('code', 'Codigo','trim|required');
            }
            $this->form_validation->set_rules('description', 'Descripcion','trim|required');
            $this->form_validation->set_rules('group_id', 'Grupo','trim|required');
            if (!isset($id)) {
                $this->form_validation->set_rules('insert_date', 'Fecha','trim|required');
            }
            $this->form_validation->set_rules('pack_desc', 'Descripcion','trim|required');
            $this->form_validation->set_rules('pack_units', 'Unidades','trim|required');
            $this->form_validation->set_rules('box_packs', 'Caja','trim|required');
            $this->form_validation->set_rules('cost', 'Costo','trim|required');
            $this->form_validation->set_rules('price', 'Precio','trim|required');
            return $this->form_validation->run();
        }
}